package com.akorena.app.bridge;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.Ostermiller.util.CmdLn;
import com.Ostermiller.util.CmdLnOption;
import com.Ostermiller.util.CmdLnResult;

/**
 * @author Ernas Moethar
 */
public class Yahoo {
	private static final Log log = LogFactory.getLog(Yahoo.class);

	
	static String server = "http://127.0.0.1:8080/smc/inbound/request.html";
	static String username = "";
	static String password = "";
	static String status = "";

	private enum EnumOptions {
		HELP(new CmdLnOption("help", 'h')), 
		SERVER(
				new CmdLnOption("server", 's')
				.setRequiredArgument()
				.setDescription("Server endpoint, default to http://127.0.0.1:8080/samba/xmlrpc")
				),
		STATUS(
				new CmdLnOption("status", 't')
				.setRequiredArgument()
				.setDescription("Sets the status of the Yahoo user using custom status")
				),	
		USERNAME(
				new CmdLnOption("username", 'u')
				.setRequiredArgument()
				.setDescription("Yahoo! username")
				), 
		PASSWORD(
				new CmdLnOption("password", 'p')
				.setRequiredArgument()
				.setDescription("Password")
				) 
				;

		private CmdLnOption option;

		private EnumOptions(CmdLnOption option) {
			option.setUserObject(this);
			this.option = option;
		}

		private CmdLnOption getCmdLineOption() {
			return option;
		}
	}

	public static void main(String[] args) throws Exception {

		CmdLn cmdLn = new CmdLn(args)
				.setDescription("Yahoo! Messenger to Spectra Bridge");
		for (EnumOptions option : EnumOptions.values()) {
			cmdLn.addOption(option.getCmdLineOption());
		}

		for (CmdLnResult result : cmdLn.getResults()) {
			switch ((EnumOptions) result.getOption().getUserObject()) {
			case HELP: {
				cmdLn.printHelp();
				System.exit(0);
			}
				break;
			case USERNAME: {
				username = result.getArgument();
			}
				break;
			case PASSWORD: {
				password = result.getArgument();
			}
				break;
			case SERVER: {
				server = result.getArgument();
			}
				break;
			case STATUS: {
				status = result.getArgument();
			}
				break;			}
		}
		
		if ((username.equals("")) || (password.equals(""))) {
			log.info("User name/password is blank. Set yahooClientInterfaceUsername/yahooClientInterfacePassword");
			return;
		}

		log.info("Yahoo! Messenger to Spectra Bridge 1.0 started with options:");
		log.info("Yahoo! username: " + username);
		log.info("Password: [supplied]");
		log.info("Endpoint server: " + server);
		
		YahooInterface ym = new YahooInterface(server, username, password, status);
		boolean loggedIn = ym.login();
		while(!loggedIn) {
			loggedIn = ym.login();
			Thread.sleep(120 * 1000);
		}

	}
	
}
	
		
	

	
